package cn.com.java.controller;

import java.util.List;

import cn.com.java.core.HttpRes;
import cn.com.java.model.Order;
import cn.com.java.model.ShoppingCart;
import cn.com.java.service.ShoppingCartService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("shoppingCart")
@RequiredArgsConstructor
public class ShoppingCartController {

    
    private final ShoppingCartService shoppingCartService;

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @RequestMapping("list")
    public List<ShoppingCart> listAll() {
        return shoppingCartService.list();
    }

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @PostMapping("listByPage")
    public HttpRes listByPage(@RequestBody ShoppingCart shoppingCart) {
        Page<ShoppingCart> page = new Page<>(shoppingCart.getPageIndex(), shoppingCart.getPageSize());
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();

        return HttpRes.OK("查询成功", shoppingCartService.page(page, shoppingCartQueryWrapper));
    }

    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 返回记录，没有返回null
     */
    @RequestMapping("getById")
    public ShoppingCart getById(Long id) {
        return shoppingCartService.getById(id);
    }    
     
    /**
     * 新增，忽略null字段
     *
     * @param shoppingCart 新增的记录
     * @return 返回影响行数
     */
    @RequestMapping("insert")
    public boolean insert(@RequestBody ShoppingCart shoppingCart) {
        return shoppingCartService.save(shoppingCart);
    }    
      
    /**
     * 修改，忽略null字段
     *
     * @param shoppingCart 修改的记录
     * @return 返回影响行数
     */
    @RequestMapping("update")
    public boolean update(@RequestBody ShoppingCart shoppingCart) {
        return shoppingCartService.updateById(shoppingCart);
    }
    
    /**
     * 删除记录
     *
     * @param shoppingCart 待删除的记录
     * @return 返回影响行数
     */
    @RequestMapping("delete")
    public HttpRes delete(Long id) {
        return HttpRes.OK(shoppingCartService.removeById(id));
    }


    /**
     * 加入购物车
     * @param userId
     * @param productId
     * @return
     */
    @RequestMapping("joinShoppingCar")
    public HttpRes joinShoppingCar(Long userId, Long productId, Integer num) {
        return HttpRes.OK(shoppingCartService.joinShoppingCar(userId, productId, num));
    }
    /**
     *
     * @param userId
     * @return
     */
    @RequestMapping("getByUserId")
    public HttpRes getByUserId(Long userId) {
        return HttpRes.OK(shoppingCartService.getByUserId(userId));
    }

}